﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEET.Exceptions.Automation;

namespace LEET.LEETCommon.AutomationAbstractions
{
    public static class PathApplier
    {
        /// <summary>
        /// Applies hierarchical conformation of search path for elements. Search path may include pipes and quotes.
        /// </summary>
        /// <exception cref="InvalidIdentifierException">Thrown if given string path does not conform to a series of paths described by (AutomationId; Name; Class), (...)...</exception>
        public static List<PathElement> Apply(String str)
        {
            if (str == null)
                str = "";
            List<PathElement> fullpath = new List<PathElement>();

            try
            {
                if (!str.StartsWith("(") && !str.EndsWith(")"))
                {
                    throw new InvalidIdentifierException();
                }
                
                String[] elements = str.Split(',');
                for (int i = 0; i < elements.Length; i++)
                {
                    string elem = elements[i].Trim();
                    int first = elem.IndexOf(';');
                    int last = elem.LastIndexOf(';');
                    if (first == -1)
                        throw new InvalidIdentifierException();
                    else if (first == last)
                    {
                        try
                        {
                            elem += "," + elements[i + 1];
                            i++;
                            last = elem.LastIndexOf(';');
                        }
                        catch (IndexOutOfRangeException)
                        {
                            throw new InvalidIdentifierException();
                        }
                    }

                    if (elem.Substring(1, first - 1).Trim().Length != 0 || elem.Substring(first + 1, last - first - 1).Trim().Length != 0)//elem.Substring(first + 2, last - first - 2)
                        fullpath.Add(new PathElement(elem.Substring(1, first - 1).Trim(),
                                                     elem.Substring(first + 1, last - first - 1).Trim(),
                                                     elem.Substring(last + 1, elem.Length - last - 2).Trim()));
                }
            }
            catch (InvalidIdentifierException)
            {
                throw new InvalidIdentifierException();
            }
            return fullpath;
        }

        /// <summary>
        /// Reconstructs the full path of the element under consideration back to a string.
        /// </summary>
        public static string AppliedString(List<PathElement> fullpath)
        {
            string ret = "";
            foreach (PathElement elem in fullpath)
                ret += elem.ToString() + ", ";
            return ret.Remove(ret.Length - 2);
        }
    }
}
